home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / DSOs / dso5.man < prev    next >
Encoding:
Text File  |  1994-08-02  |  49.8 KB  |  1,074 lines

  1.  
  2. DSO(5)                         Silicon Graphics                         DSO(5)
  3.  
  4. NAME
  5.      DSO - Dynamic Shared Object
  6.  
  7. TOPIC
  8.      This man page is intended to be both a quick reference and a source of
  9.      detailed information on Dynamic Shared Objects.  It is divided into 4
  10.      sections:
  11.  
  12.      General Information and Overview
  13.  
  14.      Linking/Building Suggestions
  15.  
  16.      Performance Considerations
  17.  
  18.      Frequently Asked Questions and Detailed Discussions
  19.  
  20. GENERAL INFORMATION AND OVERVIEW
  21.    Format
  22.      A DSO, or Dynamic Shared Object, is an ELF format object file, very
  23.      similar in structure to an executable program but with no "main".  It has
  24.      a shared component, consisting of shared text and read-only data; a
  25.      private component, consisting of data and the GOT (Global Offset Table);
  26.      several sections that hold information necessary to load and link the
  27.      object; and a liblist, the list of other shared objects referenced by
  28.      this object. Most of the libraries supplied by SGI are available as
  29.      dynamic shared objects.
  30.  
  31.    PIC -- Position Independent Code
  32.      A DSO is relocatable at runtime; it can be loaded at any virtual address.
  33.      A consequence of this is that all references to external symbols must be
  34.      resolved at runtime.  References from the private region (.e.g. from
  35.      private data) are resolved once at load-time; references from the shared
  36.      region (e.g. from shared text) must go through an indirection table (GOT)
  37.      and hence have a small performance penalty associated with them.
  38.  
  39.      Code compiled for use in a shared object is referred to as PIC whereas
  40.      non-PIC is usually referred to as non-shared. Non-shared code and PIC can
  41.      not be mixed in the same object.
  42.  
  43.    What Happens at Runtime?
  44.      Exec loads the main program and then loads rld(1), the runtime linking
  45.      loader, which finishes the exec operation.  Starting with main's liblist,
  46.      rld loads each shared object on the list that is not marked as delay
  47.      load, reads that object's liblist, and repeats the operation until all
  48.      shared objects have been loaded.  Next, rld allocates common and fixes up
  49.      symbolic references in each loaded object.  (This is necessary because we
  50.      don't know until runtime where the object will be loaded.)  Next, each
  51.      object's init code is executed.  Finally, control is transferred to
  52.      "__start".
  53.  
  54. Page 1                           Release 5.2
  55.  
  56. DSO(5)                         Silicon Graphics                         DSO(5)
  57.  
  58.      Be sure to read about quickstart, delayed loads, sgidladd(3), and
  59.      dlopen(3) as each can affect this general process.
  60.  
  61. LINKING / BUILDING DYNAMIC SHARED OBJECTS
  62.    Example
  63.      Suppose your library is in an archive libfoo.a of object files all of
  64.      which have been compiled -shared; and it references symbols found in
  65.      libc.so, libgl.so, libX11.so and libnetls.so though most programs will
  66.      never use the path that requires libnetls.so. SGI recommends building
  67.      your DSO, libfoo.so, in the following way:
  68.  
  69.           ld                           \
  70.                -elf                    \
  71.                -shared                 \
  72.                -no_unresolved          \
  73.                -rodata_shared          \
  74.                -o libfoo.so            \
  75.                -all libfoo.a           \
  76.                -lX11                   \
  77.                -delay_load -lnetls     \
  78.                -lc                     \
  79.                -lgl
  80.  
  81.      This builds a DSO called libfoo.so that will request rld to load libc.so,
  82.      libX11.so, and libgl.so  whenever libfoo.so is loaded and will load
  83.      libnetls.so if it is ever referenced.
  84.  
  85.    Controlling The Symbols Exported By A Dynamic Shared Object
  86.      In building a dynamic shared object it is necessary that the control is
  87.      maintained over which symbols are exported by that object.  One of the
  88.      benefits of using dynamic shared objects is the ability to release new
  89.      versions of that object and still have objects that were linked against
  90.      the old version, work with the new version.  This will be impossible to
  91.      guarantee, if the set of symbols exported by an object can not easily be
  92.      understood by object's creator.  ld provides several options that will
  93.      aid the developer in controlling which symbols are exported by a dynamic
  94.      shared object.
  95.  
  96.      By default ld will not export symbols that are supplied by a linked-in
  97.      archive or dynamic shared object.  In either of these cases the developer
  98.      is probably only a consumer of the linked-in object, not an exporter. In
  99.      a subsequent release, the developer may not require the linked-in object
  100.      and if the symbols provided by the linked-in object had been exported by
  101.      the developer's object, the new object would not longer be upwardly
  102.      compatible with the original version. This behavior can be overridden
  103.      using the -exports option.  This behavior, with respect to archives, is
  104.      also overriden when building a dynamic shared object from an archive
  105.      using the -all option.
  106.  
  107.      The developer can provide even greater control over the list of symbols
  108.      that are exported using options -exported_symbol, -exports_file, -
  109.      hidden_symbol, or -hides_file. The first two options lets the developer
  110.  
  111. Page 2                           Release 5.2
  112.  
  113. DSO(5)                         Silicon Graphics                         DSO(5)
  114.  
  115.      specifically list the symbols that are to be exported by the dynamic
  116.      shared object.  The -exported_symbol option is followed by a comma
  117.      separated list of names.  The -exports_file option gives a filename that
  118.      contains a space separated (including newlines) list of names.  If any
  119.      symbols are specifically exported, then only those symbols will be
  120.      exported.  All other symbols are automatically hidden.  The last two
  121.      options lets the developer specify a list of symbols that are to be not
  122.      exported by the dynamic shared object.
  123.  
  124.      There are two consequences of hiding symbols.  First, those symbols will
  125.      not provide resolution to any undefines in an object that links in the
  126.      dynamic shared object.  Second , any references to that symbol within the
  127.      dynamic shared object will be resolved internally to the hidden symbol.
  128.  
  129.    Rules Of Thumb
  130.      Use -no_resolved to find unresolved symbols.  While it is not always
  131.      possible to supply on the link line all the shared objects that will be
  132.      referenced by libfoo.so, in general, libraries should be self-contained.
  133.      This is especially true for subsequent releases of a dynamic shared
  134.      object.  If a dynamic shared object has any unresolved references, they
  135.      must be resolved by some other loaded object.  Having unresolved symbols
  136.      invites disaster since there is no guarantee that the symbols will be
  137.      resolved and thus the application will not run.
  138.  
  139.      Link against the minimum set of .so's needed.  Loading a shared object
  140.      does carry a cost.  Linking against unneeded dynamic shared objects will
  141.      cause them to be loaded even if they are never referenced.  ld will
  142.      provide a warning when you have linked against a dynamic shared object
  143.      that resolves no symbols.
  144.  
  145.      When building a c++ dynamic shared object, specify -exports for any
  146.      dynamic shared object that provides the definitions of classes that
  147.      classes in the object being created derive from.  Specifying -exports in
  148.      this case will make sure consumers of the object being created will be
  149.      able to create subclasses of classes provided by that object, without
  150.      having to know the complete set of dynamic shared objects that will need
  151.      to be loaded.  Using the -exports flag in this case may bring in unwanted
  152.      symbols.  In that case using the -exported_symbol, -exports_file, -
  153.      hidden_symbol, or -hides_file. options may be necessary.
  154.  
  155.      Use -rodata_shared to move all read-only data into the shared segment.
  156.      Unfortunately, there are many programs that write to supposedly read-only
  157.      data; for this reason, this option is off by default.  The compiler
  158.      option, -use_readonly_const, is on by default.
  159.  
  160.      If you reference the gl, have it last in the link line.  Often libgl.so
  161.      cannot be quickstarted (see below); putting it last allows all the
  162.      "upstream" objects to still be quickstarted.  You can also choose to
  163.      delay load libgl.so. This will still allow your application to
  164.      quickstart.
  165.  
  166. Page 3                           Release 5.2
  167.  
  168. DSO(5)                         Silicon Graphics                         DSO(5)
  169.  
  170.      Anytime a "downstream" shared object (a referenced object) changes, you
  171.      should relink in order to quickstart.
  172.  
  173.      Try to minimize inter-DSO data references.
  174.  
  175.      Try to minimize the use of global data.  In DSO's, it is generally more
  176.      efficient to malloc space when needed rather than use a large static data
  177.      structure.
  178.  
  179.      Try to pack data together that is likely to be unmodified.  This allows
  180.      the kernel to make more of the data pages shared, copy-on-write.
  181.  
  182.      Use the -delay_load option on any shared object on the link line that is
  183.      not often used.  This adds a small performance penalty for references to
  184.      it, but can save time and memory for those programs that don't use it.
  185.  
  186.      Do not call sproc from any code that may be executed at init time.
  187.  
  188. PERFORMANCE CONSIDERATIONS
  189.    Quickstart
  190.      When building a shared object or an executable, ld assigns addresses to
  191.      the object and attempts to resolve all references.  At runtime, if rld
  192.      verifies that the same set of objects are loaded at the original
  193.      addresses, then rld can skip all the runtime relocation work and let the
  194.      program run.  This saves time by skipping doing the relocations and saves
  195.      memory since rld does not have to read in the sections that hold the
  196.      relocation information.
  197.  
  198.      At static link time, ld will resolve any unresolved function calls to a
  199.      stub routine which references an rld function called lazy_text_resolve().
  200.      When invoked at runtime, it performs the relocation needed for all future
  201.      calls to the original function.   In this way, more programs can quick
  202.      start even if some of the function references are not resolved at static
  203.      link time.
  204.  
  205.      Quickstart will fail whenever the dynamic shared objects on a system do
  206.      not match the objects used when linking and application or the shared
  207.      objects that it depends on.  This will occur whenever a new version of a
  208.      dynamic shared object is released.  When quickstart fails rld has to do a
  209.      significant amount of work.  The rqs(1) command can be used  to
  210.      recalculate the quickstart information associated with an application or
  211.      a dynamic shared object.  rqs must be called in proper order so that
  212.      dynamic shared objects on an objects liblist are requickstarted before
  213.      the object ir requickstarted. rqs will rewrite the object it is
  214.      requickstarting back in place.  It is possible to mark an object as non-
  215.      requickstartable by using the -no_rqs flag to ld.
  216.  
  217.    Avoiding Gratuitous Shared Object Loads
  218.      Since for each shared object that is loaded, rld does a considerable
  219.      amount of work and can use up large amounts of real memory,  it is
  220.      advantageous to not link against shared objects that are not needed.
  221.  
  222. Page 4                           Release 5.2
  223.  
  224. DSO(5)                         Silicon Graphics                         DSO(5)
  225.  
  226.    dlopen(3), sgidladd(3), and delayed loads
  227.      The overhead associated with objects that are referenced but seldom
  228.      actually used can be mitigated by using dlopen(3), sgidladd(3), or
  229.      delayed loads. The use of any of these, delays the loading of a shared
  230.      object (and the objects on it's liblist) until it is actually referenced.
  231.      The most convenient is the -delay_load option to ld. All three require
  232.      that there be no references from any other objects' data section to the
  233.      delay loaded shared object.
  234.  
  235. FREQUENTLY ASKED QUESTIONS
  236. List of Questions:
  237.  
  238.    1)  What is DSO?
  239.    2)  How do dynamic shared objects compare with shared libraries?
  240.    3)  How do I maintain binary compatibility between versions of dso's?"
  241.    4)  Under which versions of the OS can I use DSO?
  242.    5)  What object-file format does DSO use?
  243.    6)  How do I install the tools so I can use DSO on my system?
  244.    7)  How do I build an executable that uses a shared object?
  245.    8)  How do I build an executable that doesn't use shared linking?"
  246.    9)  How do I tell if an executable will use dynamic linking?
  247.    10) How do I build a shared object?
  248.    11) Where does the system look for shared objects at runtime?
  249.    12) What is Quickstart?
  250.    13) What is the so_locations file?
  251.    14) What directives can be put in a so_locations file?
  252.    15) What is /usr/lib/so_locations?
  253.    16) If I don't have a valid so_locations, can I generate one from all 
  254.        the .so's in, say, /usr/lib?"
  255.    17) How expensive is it (at runtime) to NOT use the -update_registry 
  256.        option?"
  257.    18) How and when will Quickstart be used?
  258.    19) What about run-time loading under user control?
  259.    20) What benefits will I get from DSO?
  260.    21) What costs are associated with DSO?
  261.    22) What is the -KPIC option?
  262.    23) Must main programs which want to use DSO's use -KPIC for compilation?"
  263.    24) How do I change my assembly language sources to use -KPIC?
  264.    25) Can I mix IRIX 4 static shared libraries with DSO's?
  265.    26) What options do I have when building a shared object?
  266.    27) What pitfalls should I know about which are associated with DSO?"
  267.  
  268. Page 5                           Release 5.2
  269.  
  270. DSO(5)                         Silicon Graphics                         DSO(5)
  271.  
  272.    28) What should I do about a GOT overflow?
  273.    29) How are multiple versions of DSO's supported?
  274.    30) Where can I find more documentation on DSO?
  275.    1)  What is DSO?
  276.      DSO stands for Dynamic Shared Objects.  DSO provides a capability similar
  277.      to static shared libraries under Cypress and earlier versions of the
  278.      IRIX, e.g., it gives applications the ability to share the text of
  279.      heavily used libraries, which need not be included in the executable
  280.      file.  However DSO has two important distinctions from static shared
  281.      objects.
  282.  
  283.    2)  How do dynamic shared objects compare with shared libraries?
  284.      First, a dynamic shared object contains only position-independent code,
  285.      so that it may be mapped into the virtual address space of different
  286.      processes at different addresses and still be shared.  Second, dynamic
  287.      shared objects, and indeed the executable itself are mapped in by a
  288.      runtime loader, rld, which resides in memory in the same address space as
  289.      the executable.  This gives the system the ability to change the binding
  290.      of symbols during executions, at the request of the executing program.
  291.      This capability should prove useful across a large segment of
  292.      applications.
  293.  
  294.    3)  How do I maintain binary compatibility between versions of dso's?
  295.      As long as the shared objects maintain the same exported symbols, or
  296.      perhaps add new symbols without removing any or changing semantics, and
  297.      don't change exported structures, they will be binary compatible.
  298.      Ordering of symbols, routines, are irrelevant, as global data, etc.  We
  299.      are working on a system called Delta-C++ which should allow you to add to
  300.      exported classes without recompiling.
  301.  
  302.    4)  Under which versions of the OS can I use DSO?
  303.      DSO is available under IRIX versions 5.0 and later.  Programs built with
  304.      DSO will not work on earlier version of IRIX.
  305.  
  306.    5)  Which object-file format does DSO use?
  307.      DSO uses the ELF object file format, as defined in the SVR4 ABI.  ELF
  308.      objects cannot be run under IRIX 4.0.5 or earlier.
  309.  
  310.    6)  How do I install the tools so I can use DSO on my system?
  311.      IRIX 5.0 and later releases all support/use DSO's.  In order to compile
  312.      and build shared objects you will need to have the Developer's option
  313.      installed.
  314.  
  315.    7)  How do I build an executable that uses a shared object?
  316.               cc myfile.c -lmine
  317.  
  318.      This will link you with libmine.so and also with libc.so.1, if either are
  319.      available.  If no libmine.so is available, but there is a libmine.a, the
  320.      libmine.a will be used along with libc.so.1, and you will still get
  321.      dynamic linking.  If you wish to be explicit, add the -call_shared flag
  322.      to the cc line:
  323.  
  324. Page 6                           Release 5.2
  325.  
  326. DSO(5)                         Silicon Graphics                         DSO(5)
  327.  
  328.               cc -call_shared myfile.c -lmine
  329.  
  330.    8)  How do I build an executable that doesn't use shared linking?
  331.      Use the -non_shared flag:
  332.  
  333.               cc -non_shared myfile.c -lmine
  334.  
  335.    9)  How do I tell if an executable will use dynamic linking?
  336.      elfdump -o shows you the ELF program header.  This contains all the
  337.      information necessary for exec and rld to run the program/shared object.
  338.      Only a.outs which use dynamic linking will have a PHDR, INTERP, or
  339.      DYNAMIC entry.
  340.  
  341.      Here's an example, and a more detailed description of this stuff.
  342.  
  343.      % elfdump -o /bin/cat
  344.  
  345.                   ***PROGRAM HEADER***
  346. Type     Offset      Vaddr      Paddr     Filesz      Memsz      Align RWX
  347.    PHDR 0x00000034 0x00400034 0x00400034 0x000000c0 0x00000000 0x00000004 r--
  348.  INTERP 0x00000100 0x00400100 0x00400100 0x00000009 0x00000009 0x00000004 r--
  349. REGINFO 0x00000110 0x00400110 0x00400110 0x00000018 0x00000018 0x00000004 r--
  350. DYNAMIC 0x00000150 0x00400150 0x00400150 0x00000a70 0x00000a70 0x00000010 r--
  351.    LOAD 0x00000000 0x00400000 0x00400000 0x00003000 0x00003000 0x00001000 r-x
  352.    LOAD 0x00003000 0x10000000 0x10000000 0x00001000 0x00001290 0x00010000 rwx
  353.  
  354.      Each line is an entry in the program header, and refers to a "segment" of
  355.      the file.
  356.  
  357.      PHDR points to the program header itself within the file. Only
  358.           executables which use dynamic linking will have this field.
  359.  
  360.      INTERP
  361.           points to a place in the file where the name of the interpreter
  362.           required for this program is to be found.  For any ABI-conforming
  363.           object, this name will be "/usr/lib/libc.so.1".
  364.  
  365.      REGINFO
  366.           points to a place in the file where information about register setup
  367.           can be found.  Currently this mostly consists of the correct gp
  368.           value for this object.
  369.  
  370.      DYNAMIC
  371.           points to the information in the file which is needed by rld to
  372.           execute it correctly.  This information includes the liblist, a
  373.           symbol table, and other information.
  374.  
  375.      LOAD points to segments that are to be mapped into the memory image.
  376.  
  377. Page 7                           Release 5.2
  378.  
  379. DSO(5)                         Silicon Graphics                         DSO(5)
  380.  
  381.      The columns give various information about each segment.
  382.  
  383.      Offset
  384.           is the offset in the file to the beginning of the segment.
  385.  
  386.      Vaddr
  387.           is the virtual address of the beginning of the segment in the memory
  388.           image of the file, ASSUMING that it was mapped as described in the
  389.           LOAD entries
  390.  
  391.      Paddr
  392.           is the same as Vaddr in our implementation.
  393.  
  394.      Filesz
  395.           is the size of the segment in the file.
  396.  
  397.      Memsz
  398.           is the size of the segment in the memory image.  When this is larger
  399.           it is assumed to be zero-filled.
  400.  
  401.      Align
  402.           is the alignment required by this section.  If an segment is to be
  403.           mapped somewhere into memory other than at Vaddr, the new address
  404.           must be congruent to Vaddr modulo the alignment.  In the example
  405.           above, the first segment must always be loaded at a page boundary,
  406.           and the second must always be loaded at a 64K boundary.
  407.  
  408.      RWX  specifies the protections r(ead), w(rite), or x(ecute) for the
  409.           segment.
  410.  
  411.      Programs which are linked -non_shared do not have a PHDR, INTERP, or
  412.      DYNAMIC section.  Thus elfdump -o is a convenient method to determine if
  413.      a program is linked -non_shared or not.
  414.  
  415.    10) How do I build a shared object?
  416.      To begin with, build a .o or .a which contains all the routines you want
  417.      to have in your .so (shared object).  This can be done with cc -c and ar.
  418.      Then invoke ld with the -shared flag. Normally the extension .so is used
  419.      to designate shared objects.
  420.  
  421.      Here is an example:
  422.  
  423.          cc -c myobj.c
  424.          ld -shared myobj.o -o myobj.so
  425.  
  426.          -or-
  427.  
  428.          <build libmine.a the usual way.>
  429.          ld -shared -all libmine.a -o libmine.so
  430.  
  431. Page 8                           Release 5.2
  432.  
  433. DSO(5)                         Silicon Graphics                         DSO(5)
  434.  
  435.      The -all flag in the second example tells ld to include all the routines
  436.      in the library.  This is necessary since there are no undefined
  437.      references (in a main, say) which is the usual way that ld knows to
  438.      include files from an archive.
  439.  
  440.    11) Where does the system look for shared objects at runtime?
  441.      The search path for shared objects is acquired in the following order:
  442.  
  443.      1) the path of the shared object if given in the liblist,
  444.  
  445.      2) in any directories specified via the
  446.           -rpath flag when the executable was built
  447.  
  448.      3) in any directory specified by the LD_LIBRARY_PATH environment
  449.           variable, if it is defined
  450.  
  451.      4) in the directories in the default path
  452.           (/usr/lib:/lib:/lib/cc:/usr/lib/cc)
  453.  
  454.      If the _RLD_ROOT environment variable is defined, then its value is
  455.      appended to the front of any path specified by -rpath and the default
  456.      path.  _RLD_ROOT itself is also a colon(:) separated list.
  457.  
  458.      See the rld(1) manpage for details.
  459.  
  460.    12) What is Quickstart?
  461.      Quickstart is an optimization. Using the so_locations file, each shared
  462.      object is pre-relocated by ld, as if it had been loaded at the address in
  463.      the so_locations file.  That way, if nothing unusual happens when we
  464.      start up the application, all the shared objects will map at their
  465.      Quickstart addresses, and rld will not need to do a relocation pass over
  466.      them.
  467.  
  468.      If for some reason more than one shared object wishes to map the same
  469.      address, rld will move one of them to an unused address and perform a
  470.      relocation pass to fix up the address references.
  471.  
  472.      If one or more of the shared objects linked against at static link time
  473.      has changed by the time the program executes, rld will need to do extra
  474.      work to ensure that symbols have been resolved to their proper value.
  475.  
  476.    13) What is the so_locations file?
  477.      In the directory in which you build a shared object, after you've
  478.      actually built one, you will notice a file named so_locations.
  479.  
  480.      It is a registry of shared objects.  It maintains the default or
  481.      Quickstart addresses of a group of shared objects which are to cooperate
  482.      by not having their default location overlap with one another.  It is
  483.      generated and updated by ld each time it builds a shared object.
  484.  
  485. Page 9                           Release 5.2
  486.  
  487. DSO(5)                         Silicon Graphics                         DSO(5)
  488.  
  489.    14) What directives can be put in an so_locations file?
  490.      Comment line
  491.           so_name [ :st = { .text | .data | $range } base_addr,padded_size : ] *
  492.  
  493.      where
  494.           so_name         full path name (or trailing component) of a
  495.                           shared object
  496.           st              string identifying start of the segment description
  497.           .text | .data   segment types: text or data
  498.           $range           limit the range of address that can be used
  499.           base_addr       address where the segment starts
  500.           padded_size     padded size of the segment
  501.  
  502.      The following directives control the placement of new shared objects:
  503.  
  504.           $text_align_size=<align>  padding=<pad-size>
  505.           $data_align_size=<align>  padding=<pad-size>
  506.               These two directives specify the alignment and padding
  507.            requirements for text and data segments respectively.
  508.            The size value in so location is calculated based on:
  509.               (section size + padding) aligned to the section align size
  510.               The align values for text and data as well as the padding
  511.               values must be aligned to a bucket size. If not, ld will
  512.            generate a warning message and align these values to bucket
  513.            size.
  514.  
  515.           $start_address=<addr>
  516.               Specifies where to start looking for addresses to put shared
  517.               objects.
  518.  
  519.           $data_after_text=[ 1 | 0 ]
  520.               Instructs the linker to place data immediately after the text
  521.               at specified text and data alignment requirements.
  522.               We set the data_after_text to 0 if the argument of this directive
  523.               is missing.
  524.  
  525.      Also, when building a dso with the -check_registry or -update_registry
  526.      flag, and if there is already an entry corresponding to this dso in the
  527.      so_location file, the linker will try to assign the same addresses for
  528.      text and data.  However, if the size of the dso changes and does not fit
  529.      in the specified location any more, the linker will search for another
  530.      spot that fits.  If the optional $range comment is given, the linker will
  531.      only place the dso in the specified range of addresses.  If there is not
  532.      enough room, an error will be given.
  533.  
  534.    15) What is /usr/lib/so_locations?
  535.      This file represents the default layout for the system shared objects.
  536.      Developers who build shared objects may find it interesting to consult
  537.      this file, in order to avoid collisions between their shared objects and
  538.      system shared objects.  This file is absolutely irrelevant to users who
  539.      merely run programs which use shared objects.
  540.  
  541. Page 10                          Release 5.2
  542.  
  543. DSO(5)                         Silicon Graphics                         DSO(5)
  544.  
  545.      There are two options which are relevant, -update_registry, and <file>
  546.      looks at <file> and builds the current .so at a location which doesn't
  547.      conflict with anything in the file (unless the current one is listed.  -
  548.      check_registry does not write to <file>.  -update_registry <file> will
  549.      consult <file> as with -check_registry, but will attempt to write an
  550.      entry for the .so being built into <file>.  If <file> is not writable, -
  551.      update_registry turns into If <file> is not readable -check_registry and
  552.      -update_registry are ignored.
  553.  
  554.    16) If I don't have a valid so_locations file, can I generate one from all
  555.      the .so's in, say, /usr/lib?
  556.      There is no convenient method to do so.  There is no guarantee that all
  557.      the .so's in /usr/lib have been coordinated so that a consistent
  558.      so_locations file can be made from them.  So it is better to get the one
  559.      that a particular release was made with.
  560.  
  561.    17) How expensive is it (at runtime) NOT to use -update_registry option?
  562.      The costs are all startup costs.  It is very difficult to say how much a
  563.      particular executable will suffer since it depends on which shared
  564.      objects the program uses and whether they have been Quickstarted for the
  565.      same address.  When there is a conflict between two objects, one will be
  566.      moved, which means that all addresses referring to names in that object
  567.      need to be relocated.
  568.  
  569.    18) How and when will Quickstart be used?
  570.      Normally, the linker will use Quickstart unless there are unresolved
  571.      symbols at static link time.
  572.  
  573.      In every executable and every shared object is a list of objects which
  574.      were looked at at static link time -- when the object was made.  This
  575.      list also contains timestamps and checksums for each of the objects.
  576.      Various levels of extra work are required if the timestamp or checksum
  577.      has changed in the library at run-time.
  578.  
  579.    19) What about run-time loading under user control?
  580.      We support an interface known as libdl, which allows users to dynamically
  581.      load their own shared objects as needed. The calls are
  582.  
  583.      dlopen() -- open a new shared object and get a "handle" to it.
  584.  
  585.      dlsym()  -- find the value of a name defined in an object.
  586.  
  587.      dlclose()-- close a shared object.
  588.  
  589.      dlerror()-- report errors.
  590.  
  591.      sgidladd() -- functions much like dlopen however it exposes
  592.           all symbols to the rest of the program.
  593.  
  594.      Consult the individual manpages for details.
  595.  
  596. Page 11                          Release 5.2
  597.  
  598. DSO(5)                         Silicon Graphics                         DSO(5)
  599.  
  600.    20) What benefits will I get from DSO?
  601.      Executables linked with shared objects will be smaller since the shared
  602.      objects are not part of the executable file image.
  603.  
  604.      Executables which use a shared object need not be relinked if a shared
  605.      object is changed -- once the updated shared object is installed, the
  606.      executable will pick it up automatically.
  607.  
  608.      Shared libraries are much easier to build, use, and debug than static
  609.      shared libraries.
  610.  
  611.      DSO allows application designers to make more machine-independent
  612.      software.  System-dependent routines can be given a uniform interface and
  613.      a shared object which implements that interface can be built for each
  614.      different platform.  Then an actual application can be shipped as-is
  615.      ("shrink-wrapped" software) to various platforms and run on them all.
  616.  
  617.      DSO gives applications the ability to change the binding of symbols at
  618.      run time, under user control.
  619.  
  620.    21) What costs are associated with DSO?
  621.      A shared object incurs two costs, both against performance.
  622.  
  623.      At startup, there will be a startup cost while rld maps in the various
  624.      objects, performs symbol resolution, etc.  We believe this cost is small
  625.      compared to the time it takes to contact the X server, for example.
  626.      Quickstart will reduce this time for smaller applications.
  627.  
  628.      A shared object's text must be PIC (position independent code).
  629.      PICification is accomplished by the code generator (ugen) and assembler
  630.      (as), when the -KPIC flag is specified.  This is the default However, PIC
  631.      code is necessarily slower. Experiments have indicated that this speed
  632.      reduction is usually less than 5 percent, but can be as much as 15
  633.      percent.  depending on the application.  PIC code seems to be worst on
  634.      very small leaf routines which access global data.
  635.  
  636.    22) What is the -KPIC option?
  637.      This flags tells the code generator and assembler to generate PIC
  638.      directly.  The result is an object file that can be put into a DSO
  639.      without further modification
  640.  
  641.               cc -KPIC -c foo.c
  642.  
  643.      will give you a PIC object foo.o.  Other drivers (cc, pc, f77, and as)
  644.      also accept the -KPIC option.  This is the default.
  645.  
  646.      Routines written in assembly languages need to be modified before -KPIC
  647.      can be used. See the question below.
  648.  
  649.      PIC objects generated by using -KPIC must be compiled -G 0.
  650.  
  651. Page 12                          Release 5.2
  652.  
  653. DSO(5)                         Silicon Graphics                         DSO(5)
  654.  
  655.    23) Must main programs which want to use DSO's use -KPIC for compilation?
  656.      Yes.  DSO's use -KPIC so that position-independent code will be
  657.      generated.  Main programs are not generally position-independent, but
  658.      must still use the DSO calling convention when calling a routine which is
  659.      defined in a DSO. In particular, this means that a main program must have
  660.      a GOT, and the code which is generated must use it.  Therefore, modules
  661.      which will become part of main programs must be compiled -KPIC as well as
  662.      modules which become part of DSO's.
  663.  
  664.    24) How do I change my assembly language sources to use -KPIC?
  665.      Several new assembler directives are added to support generation of PIC.
  666.      You should also get yourself familiar with the MIPS ABI Supplement and
  667.      the PIC coding model it describes.  In addition, files which are to be
  668.      assembled with -KPIC must also be -G 0.  This is normally turned on by
  669.      the driver by default.
  670.  
  671.      Note that with the exception of (a) and (d), all other directives
  672.      described below will be ignored when -KPIC is not explicitly specified.
  673.      Also, item (d), ".gpword", will be turned into ".word". The result will
  674.      be a NON-PIC version of the same routine.
  675.  
  676.      a) .option pic2
  677.  
  678.      This directive forces the assembler to mark the output object file "PIC"
  679.      and activates the following directives.  It overrides the command line
  680.      argument.  Normally, you don't need to specify this directive.  Instead,
  681.      you should use the -KPIC or -non_shared flags to toggle between
  682.      generating PIC or non-PIC.
  683.  
  684.      Note that even though -KPIC will be made the default for the high-
  685.      language driver (cc/pc/f77) in future releases, it will *NOT* be the
  686.      default for assembly sources.  You will always have to explicitly specify
  687.      -KPIC for compiling .s files.
  688.  
  689.      b) .cpload reg
  690.  
  691.      This directive expands into three instructions that sets the gp register
  692.      to the context pointer value for the current function.  The three
  693.      instructions are:
  694.           lui  gp,_gp_disp
  695.           addui     gp,gp,_gp_disp
  696.           addu gp,gp,reg
  697.  
  698.      _gp_disp is a reserved symbol defined by the linker to be the distance
  699.      between the lui instruction and the context pointer.  This directive is
  700.      required at the beginning of each subroutine that uses the gp register.
  701.  
  702.      You must add this directive at the beginning of every procedure, with the
  703.      exception of leaf-procedures that do not access any global variables, and
  704.      procedures that are static (i.e., not marked .globl or .extern).
  705.  
  706. Page 13                          Release 5.2
  707.  
  708. DSO(5)                         Silicon Graphics                         DSO(5)
  709.  
  710.      c) .cprestore offset
  711.  
  712.      This directive causes the assembler to issue
  713.                sw   gp,offset(sp)
  714.      at the point where it appears.  Additionally, it causes the assembler to
  715.      emit
  716.                lw   gp,offset(sp)
  717.      after every jump-and-link (jal) or branch-and-link (bal) operation,
  718.      thereby restoring the gp register after function calls.  The programmer
  719.      is responsible for allocating the stack space for the gp.  This space
  720.      should be in the saved register area of the stack frame to remain
  721.      consistent with MIPS' calling and debugger conventions.
  722.  
  723.      d) .gpword local-sym
  724.  
  725.      This directive is similar to .word except that the relocation entry for
  726.      local-sym has the R_MIPS_GPREL32 type.  After linkage, this results in a
  727.      32-bit value that is the distance between local-sym and the context
  728.      pointer (i.e. the gp).  local-sym must be local.  It is currently used
  729.      for PIC switch tables.
  730.  
  731.      e) .cpadd reg
  732.  
  733.      This adds the value of the context pointer (gp) to reg.
  734.  
  735.      EXAMPLES:
  736.           This is a simplified version of the "hello world" program:
  737.           --------------------------------------------------------------
  738.                .option   pic2
  739.                .data
  740.                .align    2
  741.           $$5:
  742.                .ascii    "hello world\X0A\X00"
  743.                .text
  744.                .align    2
  745.           main:
  746.                .set  noreorder
  747.                .cpload   $25
  748.                .set  reorder
  749.                subu $sp, 40
  750.                sw   $31, 36($sp)
  751.                .cprestore     32
  752.                la   $4, $$5
  753.                jal  printf
  754.                move $2, $0
  755.                lw   $31, 36($sp)
  756.                addu $sp, 40
  757.                j    $31
  758.           ----------------------------------------------------------------
  759.           The actual instructions generated by the assembler will be:
  760.  
  761.                lui  gp,0      #
  762.  
  763. Page 14                          Release 5.2
  764.  
  765. DSO(5)                         Silicon Graphics                         DSO(5)
  766.  
  767.                addiu     gp,gp,0        # generated by .cpload
  768.                addu gp,gp,t9  #
  769.                lw   a0,0(gp)  # gp-relative addressing used
  770.                lw   t9,0(gp)  # t9 is used for func. call
  771.                addiu     sp,sp,-40
  772.                sw   ra,36(sp)
  773.                sw   gp,32(sp) # from .cprestore
  774.                jalr ra,t9          # jal is changed to jalr
  775.                addiu     a0,a0,0
  776.                lw   ra,36(sp)
  777.                lw   gp,32(sp) # activated by .cprestore
  778.                move v0,zero
  779.                jr   ra
  780.                addiu     sp,sp,40
  781.                nop
  782.           ----------------------------------------------------------------
  783.  
  784.      NOTE:
  785.  
  786.      The MIPS's ABI required register t9 ($25) be used for indirect function
  787.      call, so .cpload should always use $25.  No reorder mode should also be
  788.      used.  Also, programmers should make sure that t9 is dead before any
  789.      function call.
  790.  
  791.      If your program uses an indirect jump (jalr), you must also use t9 as the
  792.      jump register.
  793.  
  794.      If you have an unconditional jump to an external label:
  795.                j  _cerror
  796.          you have to rewrite it into indirect jump via t9, i.e.:
  797.                la t9,_cerror
  798.                j  t9
  799.  
  800.      If you use branch-and-link (bal) instruction, and if the target procedure
  801.      begins with a .cpload, you have to specify an alternate entry point:
  802.  
  803.           foo: .set noreorder # callee
  804.                .cpload   $25
  805.                .set reorder
  806.           $$1: ...            # alternative entry point
  807.                ...
  808.                j    $31       # foo returns
  809.  
  810.           bar: ...            # caller
  811.                ...
  812.                bal  $$1       # by-pass the .cpload
  813.                ...
  814.  
  815.      This is very important because .cpload assumes register $25 contains the
  816.      address of foo, but in this case $25 is not set up.  Note that since both
  817.      foo and bar reside in the same file, they must have the same value for
  818.      $gp.  So the .cpload instructions can be and must be bypassed.  However,
  819.  
  820. Page 15                          Release 5.2
  821.  
  822. DSO(5)                         Silicon Graphics                         DSO(5)
  823.  
  824.      since foo can still be called from outside, the .cpload is still
  825.      required.
  826.  
  827.      Alternatively, if you don't want to have an alternate entry point, you
  828.      can set up register $25 before the bal:
  829.                la   t9,foo
  830.                bal  foo
  831.  
  832.          but this will be less efficient.
  833.  
  834.      position-independent jump table (or any table of text addresses).
  835.      Entries of the address table created by .gpword are converted into
  836.      displacement from the context pointer.  To get the correct text address,
  837.      .cpadd should be used to add the value of gp back to them.  Since the gp
  838.      is updated by the run-time linker, the correct text address can be
  839.      reconstructed regardless of the location of the dso.
  840.  
  841.    25) Can I mix IRIX 4 static shared libraries with DSO's?
  842.      We do not anticipate ever allowing an a.out to use both static shared
  843.      libraries and dso.
  844.  
  845.    26) What options do I have when building a shared object?
  846.      If you specify the flag -B dynamic while linking a shared object, symbols
  847.      in the shared object will be resolved differently than the default
  848.      linkage convention.  In particular, the runtime linker will always try to
  849.      resolve any symbols referenced in that object to symbols defined in that
  850.      object first, instead of looking for definitions in objects in the order
  851.      specified on the link line.
  852.  
  853.      The effect of this is to make all symbols defined and used in such
  854.      objects non-preemptable.  Ordinarily a such symbol definitions could be
  855.      preempted by a definition in an earlier shared object, but when -B
  856.      symbolic is specified, this is not the case.
  857.  
  858.    27) What pitfalls are associated with DSO?
  859.      Behind most of the surprises that users will get is the fact that linking
  860.      semantics are fundamentally different, but only in a subtle way.  Let us
  861.      suppose that your program links with three libraries, libA, libB and
  862.      libC, in that order.  Further suppose that both libA and libC define some
  863.      symbol x, but don't use it.  Furthermore, let us suppose that libB
  864.      contains a reference to x.  Archive linking (the old way) will resolve
  865.      B's reference to x to the definition in C, whereas shared object linking
  866.      will resolve B's reference to x to the definition in A.
  867.  
  868.      Why the difference?  With archive linking, when libA is examined, there
  869.      is no outstanding reference to x, hence the definition of x is not
  870.      extracted from the archive.  Later when libC is examined, there is a
  871.      reference to x, so it is loaded.
  872.  
  873.      With shared objects, all the constituent object files have been joined
  874.      into one object, so all symbol definitions are always present.  The
  875.      resolution rule is simple, take the definition in the object listed
  876.  
  877. Page 16                          Release 5.2
  878.  
  879. DSO(5)                         Silicon Graphics                         DSO(5)
  880.  
  881.      first. Thus the definition in libA is used.
  882.  
  883.      Another sort of surprise is the "runtime dangling reference".  It is
  884.      altogether possible to build and link an application with no errors or
  885.      even warnings, only to get a message from rld stating that your program
  886.      has unresolvable symbols.
  887.  
  888.      What's going on?  Well, if you build a shared object as part of your
  889.      program, the linker will not normally complain about undefined symbols
  890.      during a link of a shared object.  This is because undefined symbols are
  891.      expected during such a build and are perfectly acceptable.  But if the
  892.      main program does not use a symbol, it does not get flagged as undefined
  893.      during static linking.  Thus the runtime "surprise".  You can use the -
  894.      no_unresolved flag to the linker to avoid such surprises.
  895.  
  896.      Now we get to a nasty pitfall which can be avoided by some cleverness in
  897.      building a shared object.  If a particular object in an archive has an
  898.      external reference to a data symbol (which it expects to be defined in
  899.      main, libl.a, for example) the linker would not try to resolve that
  900.      external unless the object file in question was actually referenced by
  901.      the main program.  Now if that archive is turned into a shared object
  902.      naively, the external data reference must be resolved whenever ANY
  903.      function in the shared object is used, even if no function in the object
  904.      file in question is ever called and no use is made of the external data
  905.      symbol in question.
  906.  
  907.      This can lead to a scenario where a user has a link that worked with the
  908.      archives, but builds a program which gets nuked by the runtime linker
  909.      under the new scheme of things.  I believe that it is a very bad idea for
  910.      us to convert libraries. such as libl.a, which contain external data
  911.      symbols, to shared objects naively.
  912.  
  913.      One thing that can be done is to split the archive into several shared
  914.      objects which are placed on the liblist of a "master" shared object.
  915.      Since rld will not by default try to resolve data symbols until the first
  916.      call is made to a particular object we can create the situation where no
  917.      attempt to resolve the offending external data symbol is made until a
  918.      call is made to the object in which it is referenced.
  919.  
  920.      Here's an example of how that works: Let us suppose that
  921.      has_extern_data.o is an object with an undefined external in it which
  922.      resides in the archive libxyz.a  Here is how to isolate that external
  923.      data reference:
  924.  
  925.      First make has_extern_data.o into a shared object all its own.
  926.  
  927.            % ar x libxyz.a has_extern_data.o
  928.            % ld -shared has_extern_data.o -o has_extern_data.so
  929.  
  930.      Now, make libxyz.so, excluding has_extern_data.o from being included
  931.      directly, but instead putting it in the liblist of libxyz.so
  932.  
  933. Page 17                          Release 5.2
  934.  
  935. DSO(5)                         Silicon Graphics                         DSO(5)
  936.  
  937.          % ld -shared -all -exclude has_extern_data.o libxyz.a has_extern_data.so      -o libxyz.so
  938.  
  939.    28) What should I do about a GOT overflow?
  940.      By default, addresses are loaded out of the Global Offset Table (GOT)
  941.      using a 16 bit offset from a context pointer.  This means that the size
  942.      of the GOT is limited (by default) to 64K bytes, or about 16 K symbols.
  943.      When there are too many symbols referenced by a DSO (or a.out) the linker
  944.      issues the messag "GOT overflows"  and will specify an object file which
  945.      references the symbol which is "out of reach".
  946.  
  947.      SGI recommends that when developers encounter this problem, they attempt
  948.      to split the DSO or a.out in question into several smaller DSO's, each of
  949.      which can conform to the GOT size limit.  Maximum performance can be
  950.      achieved this way.
  951.  
  952.      However, as an alternative, developers may wish to use the -xgot
  953.      compile-time flag to tell the compiler to issue a different (and slower)
  954.      code sequence uses a 32-bit offset.  This will allow the GOT to contain
  955.      up to 1G entries.  However, it is critical that every object linked into
  956.      a final DSO or a.out be compiled with -xgot turned on, otherwise code may
  957.      have been generated which will not work with an extended GOT.  However,
  958.      files compiled with -xgot may be linked into a DSO or a.out which has a
  959.      GOT that does not exceed the 16K limit and will work correctly, if
  960.      somewhat slower.  The GOT size of any shared objects linked is
  961.      irrelevant.
  962.  
  963.      The directory /usr/lib/xgot contains the extended-GOT versions of those
  964.      objects which SGI has built both normally and -xgot. If a system or third
  965.      party archive contains small GOT objects which are needed in an extended
  966.      GOT link, a developer can take the following steps: 1) Look in
  967.      /usr/lib/xgot to see if an extended GOT version exists.  2) Turn the
  968.      archive into its own shared object, thus isolating it from the extended
  969.      GOT binary. 3) contact the archive provider.  In a few cases (crt1.o,
  970.      crtn.o, c++init.o, and fixade.o), where the performance issues were
  971.      minimal,  the default objects in /usr/lib are in fact built large GOT.
  972.  
  973.    29) How are multiple versions of DSO's supported?
  974.      IRIX 5.0.1 (Compilers v3.16) and later supports the ability to tag shared
  975.      objects and executables with a version number.  This is intended to
  976.      support interface changes.  Details are below; items marked (SGI ONLY) do
  977.      not apply to MSIG ABI binaries, but only to binaries generated on IRIX
  978.      without the -abi flag turned on.
  979.  
  980.     Versioning of Shared Objects.
  981.  
  982.     QUICK OVERVIEW
  983.  
  984.     In order for a shared object to be versioned in the future
  985.     the following needs to be done:
  986.  
  987.     * Version strings consist of 3 parts and a dot: The string "sgi",
  988.       a decimal number (the major number), a dot, and a decimal number
  989.  
  990. Page 18                          Release 5.2
  991.  
  992. DSO(5)                         Silicon Graphics                         DSO(5)
  993.  
  994.       (the minor number).
  995.  
  996.     * Add the command -set_version sgi1.0 to the command to build
  997.       the shared object (cc -shared, ld -shared, etc.).
  998.  
  999.     * (Future) Whenever you make a COMPATIBLE change update the minor version
  1000.       number (the one after the dot), and add the latest version string
  1001.       to colon-separated list of version strings, e.g., -set_version
  1002.       sgi1.0:sgi1.1:sgi1.3
  1003.  
  1004.     * (Future) Whenever you make an INCOMPATIBLE change, update the
  1005.       major version number.  Pass this as the version list, e.g.,
  1006.       -set_version sgi2.0.  Change the filename of the OLD shared object
  1007.       by adding a dot followed by the previous major number to the filename
  1008.       of the shared object.  DO NOT CHANGE the soname of the object.
  1009.       No change to the file contents are necessary or desirable.  Simply
  1010.       rename the file.
  1011.  
  1012.     HOW IT ALL WORKS
  1013.  
  1014.     * Versioning will only be available for NON-ABI executables.
  1015.       The current ABI does not require objects to have versioning, nor
  1016.       does it require systems to pay attention to versioning.  It does
  1017.       allow objects to contain version strings, but does not require
  1018.       systems to do anything with this information.
  1019.  
  1020.     * NON-ABI compliant executables will have a SGI_ONLY bit turned
  1021.       on in the .dynamic section.  This flag will be understood and
  1022.       reported by elfdump.  Only executables with this flag on will
  1023.       get the versioning treatment described below.  This flag will
  1024.       be on by default.
  1025.  
  1026.     * When an executable is linked against a shared object, the last
  1027.       entry of the shared object's version string is recorded in the
  1028.       executable as part of the liblist.  This can be examined by
  1029.       elfdump -Dl.
  1030.  
  1031.     * When an executable is linked, the user may specify -require_minor or
  1032.       -ignore_minor for each shared object linked against.  If
  1033.       -require_minor is specified, a bit will be set in the flags field of
  1034.       the liblist entry for the shared object in question.  The default
  1035.       is -ignore_minor.
  1036.  
  1037.     * When an executable (ABI or SGI_ONLY) is run rld will look
  1038.       for the proper filename in its usual search routine.
  1039.  
  1040.     * (SGI_ONLY) If a file with the correct name is found the
  1041.       version string in the liblist is compared to the list
  1042.       of version strings in the shared object.  If the REQUIRE_MINOR bit
  1043.       is set in the liblist entry, and there is an exact match between the
  1044.       version string in the depender and one of the strings in the version
  1045.       list of the dependee, then that library is used.  If the
  1046.  
  1047. Page 19                          Release 5.2
  1048.  
  1049. DSO(5)                         Silicon Graphics                         DSO(5)
  1050.  
  1051.       REQUIRE_MINOR bit is clear, and if there is a match of major
  1052.       versions, then that library is used.
  1053.  
  1054.     * (SGI_ONLY) If no proper match is found, a new soname is built
  1055.       by taking the soname found in the executables liblist, and the
  1056.       major number found in the version string corresponding to that
  1057.       liblist entry, and putting them together as <soname>.<major>
  1058.       This is searched for in the same way as above. Version strings are
  1059.       matched in exactly the same way as described above.
  1060.  
  1061.    30) Where can I find more documentation on DSO?
  1062.      Besides the other manpages mentioned below, System V Application Binary
  1063.      Interface and System V Application Binary Interface -- are both good
  1064.      sources of DSO implementation details.
  1065.  
  1066. SEE ALSO
  1067.      rld(1), ld(1), elf(5), elfdump(1), dlopen(3), sgidladd(3)
  1068.  
  1069. UPDATES
  1070.      This man page is periodically updated;  the last update done on
  1071.      1993/11/08 for IRIX 5.2.
  1072.  
  1073. Page 20                          Release 5.2
  1074.